package com.youhome.clients.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.Principal;
import com.youhome.service.service.OrderService;
import com.youhome.service.service.PayService;
import com.youhome.utils.IPUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;


/**
 * Created by chen365 on 2017/9/13.
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private PayService payService;

    @ApiOperation(value = "订单列表", notes = "订单列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", required = true, paramType = "path")
    })
    @GetMapping(value = "/list/{pageNumber}")
    public Object list(@PathVariable("pageNumber") Integer pageNumber,
                       @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        Integer pageSize = 10;
        return new ResultPage<>(ResultCode.SUCCESS, orderService.selectByUserId(principal,pageNumber, pageSize));
    }


    @ApiOperation(value = "提交订单", notes = "提交订单", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long", name = "productId", value = "商品ID", required = true, paramType = "path")
    })
    @PostMapping(value = "/submit/{productId}")
    public Object submitOrder(HttpServletRequest request, @PathVariable Long productId,
                              @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        String ip = new IPUtils().getRemoteAddrIp(request);
        return new Result<>(ResultCode.SUCCESS, orderService.submitOrder(principal, productId, ip));
    }

    @ApiOperation(value = "订单详情", notes = "订单详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "orderNo", value = "订单ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/detail/{orderNo}")
    public Object getOrder(@PathVariable(value = "orderNo") String orderNo,
                           @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, orderService.selectByOrderNo(principal, orderNo));
    }

    @ApiOperation(value = "提交订单到ping++ 获取支付二维码", notes = "提交订单到ping++ 获取支付二维码", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "credential", value = "支付方式", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "orderNo", value = "订单ID", required = true, paramType = "path")
    })
    @PostMapping(value = "/pay/{credential}/{orderNo}")
    public Object orderPay(HttpServletRequest request,
                           @PathVariable(value = "credential") Integer credential,
                           @PathVariable(value = "orderNo") String orderNo,
                           @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {

        String ip = new IPUtils().getRemoteAddrIp(request);
        return new Result<>(ResultCode.SUCCESS, payService.scanCodePay(principal,orderNo, ip, credential));
    }

    @ApiOperation(value = "查询订单是否支付成功", notes = "查询订单是否支付成功", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "orderNo", value = "订单ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/status/{orderNo}")
    public Object searchOrderStatus(@PathVariable(value = "orderNo") String orderNo,
                                    @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        orderService.searchOrderStatus(principal, orderNo);
        return new Result<>(ResultCode.SUCCESS);
    }
}
